//
//  ViewController.m
//  11104
//
//  Created by student on 16/3/6.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) UIScrollView *scrollView;

@property (nonatomic,weak) UIView *moveView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.scrollView = [self setUpScrollView];
    [self setUpBtn];
    self.moveView = [self setUpView];
}

-(UIScrollView *)setUpScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, 60)];
    scrollView.contentSize = CGSizeMake(94*10, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:scrollView];
    return scrollView;
}

-(void )setUpBtn{
    for (int i = 0; i < 10; i++) {
        for (int i=0; i<10; i++)
        {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(94*i, 0, 80, 50)];
            [btn setTitle:[NSString stringWithFormat:@"新闻%i",i+1] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
            btn.tag = 100+i;
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
            
            if(i==0)
            {
                btn.selected = YES;
            }
            
            [self.scrollView addSubview:btn];
        }

    }
}

-(UIView *)setUpView{
    UIView *moveView = [[UIView alloc] initWithFrame:CGRectMake(15, 48, 50, 10)];
    moveView.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:moveView];
    return moveView;
}
-(void)btnPressed:(UIButton *)sender{
    for (UIButton *btn in self.scrollView.subviews){
        if ([btn isKindOfClass:[UIButton class]]) {
            UIButton *tmpBtn = btn;
            tmpBtn.selected = NO;
        }
    }
    sender.selected = YES;
    
    // 3. 移动滑块
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    int index = sender.tag - 100;
    self.moveView.frame = CGRectMake(94*index+15, 48, 50, 10);
    [UIView commitAnimations];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
